/*
 * step.c
 *
 *  Created on: 2015-03-12
 *      Author: root
 */

#include "step.h"

int _gCurrentStep = 0;
int _gStepPos = 0;
int _gNextDelay = 50;
int _gStepDir=0;

int basePeriod=0;

//First 50
int _gStepMap[] = { 18,18,18,17,17,17,16,16,15,15,15,14,14,14,13,13,13,12,12,11,11,10,10,9,9,
8,8,7,7,6,5,5,5,5,5,5,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
//Second50
18,18,18,17,17,17,16,16,15,15,15,14,14,14,13,13,13,12,12,11,11,10,10,9,9,
8,8,7,7,6,5,5,5,5,5,5,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
//The 100
18,18,18,17,17,17,16,16,15,15,15,14,14,14,13,13,13,12,12,11,11,10,10,9,9,
8,8,7,7,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
//padding
5,5,5,5,5
};

char msg[] = {"Step "};

//internal functions
void initStepTimer(void);
void initStepGPIO(void);
void smallStep(int);
void step(int dir);
void stepPos(int dir);
//end Internal Functions


//Externally Accessible Functions

void initStepper(void)
{
	//set up peripherals
	//
	initStepGPIO();
	initStepTimer();
	//calibrateStepper();

}

void stepperHandler(void)
{
	//ROM_TimerLoadSet(TIMER2_BASE, TIMER_A, ((_gStepMap[_gStepPos])*basePeriod));
	//int temp=ulADC0_Value[0];
	ROM_TimerLoadSet(TIMER2_BASE, TIMER_A, _gNextDelay*basePeriod*10);//always multiply by base period
	ROM_TimerIntClear(TIMER2_BASE, TIMER_TIMA_TIMEOUT);
	stepPos(1);
	step(_gStepDir);
	bt_UARTsend(msg);
	bt_UARTputc(48+_gCurrentStep);
	bt_UARTsend("\r\n");
}










void smallStep(int coil)
{
	//define pins here for future reassignment
	#define E1 PA_7 	//PB_5
	#define L1 PA_6		//PB_0
	#define L2 PA_5		//PB_1
	#define E2 PD_3		//PE_4
	#define L3 PD_2		//PE_5
	#define L4 PD_1		//PE_1
	#define Low 0x00
	switch(coil)
	{
		case 1: {
				ROM_GPIOPinWrite(GPIO_PORTA_BASE, (E1 | L1 | L2), (E1 | L1));
				ROM_GPIOPinWrite(GPIO_PORTD_BASE, (E2 | L3 | L4), E2);
			} break;
		case 2: {
				ROM_GPIOPinWrite(GPIO_PORTA_BASE, (E1 | L1 | L2), E1);
				ROM_GPIOPinWrite(GPIO_PORTD_BASE, (E2 | L3 | L4), (E2 | L3));

			} break;
		case 3: {
				ROM_GPIOPinWrite(GPIO_PORTA_BASE, (E1 | L1 | L2), (E1 | L2));
				ROM_GPIOPinWrite(GPIO_PORTD_BASE, (E2 | L3 | L4), E2);

			} break;
		case 4: {
				ROM_GPIOPinWrite(GPIO_PORTA_BASE, (E1 | L1 | L2), E1);
				ROM_GPIOPinWrite(GPIO_PORTD_BASE, (E2 | L3 | L4), (E2 | L4));

			} break;
		default: bt_UARTsend("SmallStep Failed"); UARTsend("Stepper motor init failed");
	}
}





void step(int dir)
{
	if (dir)
	{
		_gCurrentStep++;
		if (_gCurrentStep >4)
		{
			_gCurrentStep=1;
		}
	}
	else
	{
		_gCurrentStep--;
		if (_gCurrentStep < 1)
		{
			_gCurrentStep=4;
		}
	}
	smallStep(_gCurrentStep);
}
void stepPos(int dir)
{
	if (dir)
	{
		_gStepPos++;
		if (_gStepPos >199)
		{
			_gStepPos=0;
		}
	}
	else
	{
		_gStepPos--;
		if (_gStepPos < 0)
		{
			_gStepPos=199;
		}
	}
	//smallStep(_gStepPos);
}

//Santos L Halper Functions
void initStepTimer(void)	//init timer
{
	//int ulPeriod=10;//FIXME
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
	ROM_TimerConfigure(TIMER2_BASE, TIMER_CFG_PERIODIC);	//set timer for periodic mode
	basePeriod = (ROM_SysCtlClockGet() / 1 / 1000);		//for 100uS
	ROM_TimerLoadSet(TIMER2_BASE, TIMER_A, basePeriod - 1);	//for 1s


	//Timer interrupt setup
	ROM_TimerIntClear(TIMER2_BASE, TIMER_TIMA_TIMEOUT);	//clear the interrupt
	ROM_TimerIntEnable(TIMER2_BASE, TIMER_TIMA_TIMEOUT);	//Enable the timer interrupt
	ROM_TimerEnable(TIMER2_BASE, TIMER_A);				//Enable the timer
	ROM_IntEnable(INT_TIMER2A);							//enable the interrupt

	//bt_UARTsend("Timer2 - Stepper Loaded\r\n");		//Confirm over UART
}


void initStepGPIO(void)
{
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, (PD_1 | PD_2 | PD_3));//Enable GPIO
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, (PA_5 | PA_6 | PA_7));//Enable GPIO
}
